package com.lys.blog.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@Configuration
@EnableAsync  //开启多线程
public class ThreadPoolConfig {

    @Bean("taskExecutor")
    public Executor asyncExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
    //    核心线程池数量
        taskExecutor.setCorePoolSize(5);
    //    最大线程数
        taskExecutor.setMaxPoolSize(20);
    //    设置队列大小
        taskExecutor.setQueueCapacity(Integer.MAX_VALUE);
    //    线程活跃时间
        taskExecutor.setKeepAliveSeconds(60);
    //    设置默认线程名称
        taskExecutor.setThreadNamePrefix("缘分锝天空博客项目");
    //    等待所有任务结束后结束线程池
        taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
    //    执行初始化
        taskExecutor.initialize();
        return taskExecutor;
    }
}
